package com.jxc.jxc01.hch.web;

import com.alibaba.fastjson2.JSONObject;
import com.jxc.jxc01.hch.entity.Inventory;
import com.jxc.jxc01.hch.entity.PageHelper;
import com.jxc.jxc01.hch.entity.RespResult;
import com.jxc.jxc01.hch.entity.User;
import com.jxc.jxc01.hch.service.IUserService;
import com.jxc.jxc01.hch.service.Impl.IUserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("*.user")
public class UserServlet extends HttpServlet {
    IUserService userService = new IUserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String uri = req.getRequestURI();
        System.out.println(uri);

        switch (uri){
            case "/jxc01/delete.user":
                delete(req,resp);
                break;
            case "/jxc01/increase.user":
                increase(req,resp);
                break;
            case "/jxc01/list.user":
                list(req,resp);
                break;
            case "/jxc01/query.user":
                query(req,resp);
                break;
            case "/jxc01/modify.user":
                modify(req,resp);
                break;
            case "/jxc01/reductionPassword.user":
                reductionPassword(req,resp);
                break;
            case "/jxc01/role.user":
                role(req,resp);
                break;
        }
    }

    /**
     * 修改用户权限
     * @param req
     * @param resp
     */
    private void role(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
            int id = Integer.parseInt(req.getParameter("id"));
            String power = req.getParameter("power");
            userService.role(id,power);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 重置密码
     * @param req
     * @param resp
     */
    private void reductionPassword(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PrintWriter out = resp.getWriter();
            int id = Integer.parseInt(req.getParameter("id"));
            userService.reductionPassword(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改用户
     * @param req
     * @param resp
     */
    private void modify(HttpServletRequest req, HttpServletResponse resp) {
        try {
            User user = new User();
            //获取请求数据
            user.setId(Integer.parseInt(req.getParameter("id")));
            user.setUsername(req.getParameter("username"));
            user.setPassword(req.getParameter("password"));
            user.setName(req.getParameter("name"));
            user.setSex(req.getParameter("sex"));
            user.setEmail(req.getParameter("email"));
            user.setPhone(req.getParameter("phone"));
            user.setState(req.getParameter("state"));
            user.setAuthority(req.getParameter("authority"));
            userService.modify(user);
            resp.sendRedirect("yuangongguanli.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 模糊查询
     * @param req
     * @param resp
     */
    private void query(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageHelper<User> pageHelper = new PageHelper<>();
            PrintWriter out = resp.getWriter();
            //获取页码页容
//            int pageNumber = Integer.parseInt(req.getParameter("pagenumber"));
//            int pageSize = Integer.parseInt(req.getParameter("pagesize"));
            int pageNumber=1;
            int pageSize=10;
            String name = req.getParameter("name");
            pageHelper.setPageNum(pageNumber);
            pageHelper.setPageSize(pageSize);
            pageHelper = userService.query(pageHelper,name);
            HttpSession session = req.getSession();
            session.setAttribute("query",pageHelper);
            RespResult respResult = new RespResult(0,"ok",pageHelper.getTotal(),pageHelper.getData());
            out.print(JSONObject.toJSONString(respResult));
            resp.sendRedirect("yuangongguanli.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询所有记录
     * @param req
     * @param resp
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageHelper<User> pageHelper = new PageHelper<>();
            PrintWriter out = resp.getWriter();
            //获取页码页容
            int pageNumber = Integer.parseInt(req.getParameter("pagenumber"));
            int pageSize = Integer.parseInt(req.getParameter("pagesize"));
            pageHelper.setPageNum(pageNumber);
            pageHelper.setPageSize(pageSize);
            HttpSession session = req.getSession();
            if (session.getAttribute("query")==null) {
                pageHelper = userService.list(pageHelper);
            }else {
                pageHelper= (PageHelper<User>) session.getAttribute("query");
            }
            RespResult respResult = new RespResult(0,"ok",pageHelper.getTotal(),pageHelper.getData());
            out.print(JSONObject.toJSONString(respResult));
            session.removeAttribute("query");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 用户添加
     * @param req
     * @param resp
     */
    private void increase(HttpServletRequest req, HttpServletResponse resp) {
        try {
            User user = new User();
            //获取请求数据
//            user.setDepartmentid(Integer.parseInt(req.getParameter("departemtid")));
            user.setUsername(req.getParameter("username"));
            user.setPassword(req.getParameter("password"));
            user.setName(req.getParameter("name"));
            user.setSex(req.getParameter("sex"));
            user.setEmail(req.getParameter("email"));
            user.setPhone(req.getParameter("phone"));
            user.setState(req.getParameter("state"));
            user.setAuthority(req.getParameter("authority"));
            userService.increase(user);
            resp.sendRedirect("yuangongguanli.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除用户
     * @param req
     * @param resp
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            int id = Integer.parseInt(req.getParameter("id"));
            userService.delete(id);
            resp.sendRedirect("yuangongguanli.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
